package com.everflourish.yeah100.adapter;

import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.everflourish.yeah100.R;
import com.everflourish.yeah100.act.questionlib.QuestionFavoriteActivity;
import com.everflourish.yeah100.entity.question.QuestionTag;
import com.everflourish.yeah100.entity.question.QuestionTagModel;
import com.everflourish.yeah100.utils.ResourcesUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Created by liqisi on 2016/7/26.
 * 时间：2016/7/26
 */
public class QuestionTagsAdapter extends RecyclerView.Adapter<QuestionTagsAdapter.QuestionTagHolder> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private ArrayList<QuestionTagModel> mList;
    private QuestionFavoriteActivity mActivity;
    private int mSelectCount = 0;

    public QuestionTagsAdapter(Context mContext, QuestionFavoriteActivity mActivity, List<QuestionTagModel> mList) {
        this.mContext = mContext;
        this.mActivity = mActivity;
        this.mList = (ArrayList<QuestionTagModel>) mList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public QuestionTagHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_question_tags, parent, false);
        QuestionTagHolder viewHolder = new QuestionTagHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(QuestionTagHolder holder, final int position) {
        QuestionTagModel questionTagModel = mList.get(position);
        QuestionTag questionTag = questionTagModel.getQuestionTag();
        holder.tagName.setText(questionTag.getTagName());
//        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
//            holder.itemLl.setBackground(ResourcesUtil.getDrawable(mContext, R.drawable.rect_list_ripple));
//        } else {
//            holder.itemLl.setBackgroundDrawable(ResourcesUtil.getDrawable(mContext, R.drawable.rect_list_ripple));
//        }
        holder.itemLl.setOnClickListener(new MyOnclickListener(holder, position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class QuestionTagHolder extends RecyclerView.ViewHolder {

        private TextView tagName;
        private LinearLayout itemLl;

        public QuestionTagHolder(View itemView) {
            super(itemView);
            tagName = (TextView) itemView.findViewById(R.id.question_tag_name);
            itemLl = (LinearLayout) itemView.findViewById(R.id.item_question_tag_ll);
        }
    }

    class MyOnclickListener implements View.OnClickListener {

        private QuestionTagHolder viewHolder;
        private int position;

        public MyOnclickListener(QuestionTagHolder viewHolder, int position) {
            this.viewHolder = viewHolder;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.item_question_tag_ll:
                    mActivity.mQuestionTagRuleTv.setTextColor(ResourcesUtil.getColor(mContext, R.color.question_appbar_Accent));
                    boolean isChecked = mList.get(position).isChecked();
                    if (isChecked) {
                        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
                            viewHolder.itemLl.setBackground(ResourcesUtil.getDrawable(mContext, R.drawable.rect_list_ripple));
                        } else {
                            viewHolder.itemLl.setBackgroundColor(ResourcesUtil.getColor(mContext,
                                    R.color.message_item_double));
                        }
                        mList.get(position).setIsChecked(false);
                        mSelectCount--;
                    } else {
                        if (mSelectCount == 5) {
                            mActivity.mQuestionTagTil.setError("不能超过五个标签");
                            return;
                        } else {
                            mActivity.mQuestionTagTil.setErrorEnabled(false);
                            mActivity.mQuestionTagTil.setError(null);
                        }
                        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
                            viewHolder.itemLl.setBackground(ResourcesUtil.getDrawable(mContext, R.drawable.rect_list_ripple_selected));
                        } else {
                            viewHolder.itemLl.setBackgroundColor(ResourcesUtil.getColor(mContext,
                                    R.color.message_item_selected));
                        }
                        mList.get(position).setIsChecked(true);
                        mSelectCount++;
                    }
                    StringBuffer stringBuffer = new StringBuffer();
                    for (QuestionTagModel model : mList) {
                        if (model.isChecked()) {
                            stringBuffer.append(model.getQuestionTag().getTagName() + " ");
                        }
                    }
                    mActivity.mQuestionTagTv.setText(stringBuffer.toString());
                    mActivity.mQuestionTagTv.setSelection(mActivity.mQuestionTagTv.getText().length());
                    break;
            }
        }
    }
}